package com.xx;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;

public class xsdasdasd {
    public static String httpclientPost(String jenkinsUser, String jenkinsPass, String pduName,String jobName,
                                        String buildJobParams,String type) throws ClientProtocolException, IOException {
        String jenkinsBuildUrl = "";

        if (type=="jenkinsBuildJob"&&buildJobParams != null) {

            jenkinsBuildUrl= String.format("http://127.0.0.1:8080/jenkins/%s/job/%s/buildWithParameters? %s",pduName,jobName,buildJobParams);
        }

        if(type=="jenkinsBuildJob") {
            jenkinsBuildUrl = "http://localhost:8080/jenkins" + "/job/" + jobName + "/build";
        }if(type=="delFolder") {
            jenkinsBuildUrl=String.format("http://127.0.0.1:8080/jenkins/view/all/job/%s/doDelete", pduName);
        }
        System.out.println(">>>>>>>>>>>>" + jenkinsBuildUrl);

        URI uri = URI.create(jenkinsBuildUrl);
        HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(jenkinsUser, jenkinsPass));

        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local auth cache
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(host, basicAuth);
        CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
        HttpGet httpGet = new HttpGet(uri);
        // Add AuthCache to the execution context
        HttpClientContext localContext = HttpClientContext.create();
        localContext.setAuthCache(authCache);
        HttpResponse response = httpClient.execute(host, httpGet, localContext);
        String result = EntityUtils.toString(response.getEntity());
        System.out.println(result);

        int responseCode = response.getStatusLine().getStatusCode();
        // String result = EntityUtils.toString(response.getEntity());
        System.out.println(responseCode);
        String code=String.valueOf(responseCode);
        return code;
    }

    public static void main(String []args) throws IOException {
        httpclientPost("admin","admin","1","testJenkinsAPI",null,"jenkinsBuildJob");
    }
}
